//
//  Database.m
//  LABEGCO
//
//  Created by Forensic on 11/19/2556 BE.
//  Copyright (c) 2556 STUDENT. All rights reserved.
//

#import "Database.h"

static Database* sharedInstance=nil;

@implementation Database


void createDB(){
    Database* db=[Database new];
    NSFileManager* fm=[NSFileManager defaultManager];
    NSError* err=nil;
    NSURL* docurl=[fm URLForDirectory:NSApplicationSupportDirectory
                             inDomain:NSUserDomainMask
                    appropriateForURL:nil
                               create:YES error:&err];
    db->databasePathString =[docurl URLByAppendingPathComponent:@"driveDB.db"].path;
    if([fm fileExistsAtPath:db->databasePathString]==NO){
        //start new DB
        const char *databasePath=[db->databasePathString UTF8String];
        if(sqlite3_open(databasePath, &(db->database))== SQLITE_OK){
            char *errMSG;
            const char *sql_stmt="CREATE TABLE IF NOT EXISTS 'DriveRecord' ('RecordID' INTEGER PRIMARY KEY AUTOINCREMENT  NOT NULL UNIQUE , 'Date' DATETIME NOT NULL )";
            if(sqlite3_exec(db->database,sql_stmt, NULL, NULL, &errMSG)!=SQLITE_OK){
                NSLog(@"Fail Create");
            }
           sql_stmt= "CREATE  TABLE 'main'.'DriveInfo' ('RecordID' INTEGER NOT NULL , 'timestamp' DATETIME NOT NULL , 'Lat' DOUBLE NOT NULL , 'Long' DOUBLE NOT NULL , 'Force' DOUBLE NOT NULL , PRIMARY KEY ('RecordID', 'timestamp'))";
            if(sqlite3_exec(db->database,sql_stmt, NULL, NULL, &errMSG)!=SQLITE_OK){
                NSLog(@"Fail Create");
            }
            sqlite3_close(db->database);
        }
        else{
            NSLog(@"Fail open/create");
        }
    }
    sharedInstance =db;
}

-(NSInteger)createRecord:(NSDate *)date{
    NSInteger recordID=-1;
    const char *databasePath=[databasePathString UTF8String];
    if(sqlite3_open(databasePath, &(database))==SQLITE_OK){
        char *errMSG;

        NSString *stmtString =[NSString stringWithFormat:@"INSERT INTO 'DriveRecord'(Date) VALUES ('%f')",date.timeIntervalSince1970];
        
        const char *sql_stmt= [stmtString UTF8String];
        
        if(sqlite3_exec(database,sql_stmt, NULL, NULL, &errMSG)!=SQLITE_OK){
            NSLog(@"Fail Create");
        }
        else{
            stmtString =[NSString stringWithFormat:@"SELECT Max(RecordID) FROM DriveRecord"];
            sql_stmt=[stmtString UTF8String];
            sqlite3_prepare_v2(database, sql_stmt
                               , -1, &statement, NULL);
            if(sqlite3_step(statement)==SQLITE_ROW){
                recordID=sqlite3_column_int(statement, 0);
                
            }
            sqlite3_finalize(statement);
        }
        sqlite3_close(database);
        
    }
    else{
        NSLog(@"Fail");
    }

    return recordID;
}

+(Database*) getSharedInstance{
    if(sharedInstance==nil){
        createDB();
    }
    return sharedInstance;
}


//count
-(NSInteger) countRecord{
return 0;
}
@end
